<template>
  <el-menu
    default-active="2"
    class="el-menu-vertical-demo"
    router
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
    :collapse="isCollapse"
  >
  <h3 v-show="!isCollapse">echats后台管理系统</h3>
  <h3 v-show="isCollapse">echarts</h3>
    <!-- 一级渲染 -->
    <el-menu-item
      :index="item.path"
      v-for="item in noChildren"
      :key="item.path"
      @click="clickMenu(item)"
    >
      <i :class="'el-icon-'+ item.icon"></i>
      <span slot="title">{{item.label}}</span>
    </el-menu-item>
    <!-- 二级渲染  -->
    <el-submenu index="1" v-for="(item,i) in hasChildren" :key="i">
      <template slot="title">
        <i :class="'el-icon-' +item.icon "></i>
        <span>{{item.label}}</span>
      </template>
      <el-menu-item-group>
        <el-menu-item
          :index="subItem.path"
          v-for="(subItem,index) in item.children"
          :key="index"
          @click="clickMenu(subItem)"
        >  
        <i :class="'el-icon-'+ subItem.icon"></i>
      <span slot="title">{{subItem.label}}</span>
      </el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<script>
export default {
  name: "Aside",
  computed: {
    //提高性能直接找打值而不是全部渲染
    noChildren() {
      //   返回不存在chidren的数组 过滤
      return this.menu.filter((item) => !item.children);
    },
    hasChildren() {
      //   返回存在chidren的数组 过滤
      return this.menu.filter((item) => item.children);
    },
    //折叠栏在计算属性里面取出vuex里面的值  this.$store.state全局的state  tab模块 变量属性
    isCollapse(){
      return this.$store.state.tab.isCollapse
    },
    menu() {
       return this.$store.state.tab.menu
    }
  },
  data() {
    return {
      //导航菜单动态 做权限判断 返回路由 通过数组来循环处理
      asideMenu: [
        {
          path: "/",
          name: "home",
          label: "首页",
          icon: "s-home",
        },
        {
          path: "/video",
          name: "video",
          label: "视频管理",
          icon: "video-play",
        },
        {
          path: "/user",
          name: "user",
          label: "用户管理",
          icon: "user",
        },
        {
          // 预防二级菜单二级页面 怎么展示处理 一级没有路由
          label: "其他",
          icon: "user",
          children: [
            {
              path: "/page1",
              name: "page1",
              label: "页面1",
              icon: "setting",
            },
            {
              path: "/page2",
              label: "页面2",
              name: "page2",
              icon: "setting",
            },
          ],
        },
      ],
    };
  },
  methods: {
    clickMenu(item) {
      this.$router.push({name:item.name})
      //通过commit进行vuex数据的替换
      this.$store.commit("selectMenu", item);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-menu {
  height: 100%;
  border: none;
  h3 {
    color:#ffff;
    text-align: center;
    line-height: 48px;
  }
}
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
</style>